/*
 *  gl21errors.c
 *  DeferredLighting
 *
 *  Created by Holmes Futrell on 2/8/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#import "platform.h"

#if TARGET_OS_MAC

#include "rb_error_checking.h"
#include <stdio.h>
#include <stdlib.h>

void fatalError(const char *error) {
	printf("fatalError: %s\n", error);
	fflush(stdout);
	exit(1);
}

const char *rbunny_framebuffer_status_to_string(GLenum fbstatus) {

	switch (fbstatus) {
		case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: return "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT";
		case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: return "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT";
		case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: return "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT";
		case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: return "GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT";
		case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: return "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT";
		case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: return "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT";
		case GL_FRAMEBUFFER_UNSUPPORTED_EXT: return "GL_FRAMEBUFFER_UNSUPPORTED_EXT";
		default: return "unknown framebuffer status";
			
	}
	
}

GLenum inner_rb_check_error( const char *file, int line, const char *s ) {
#if RB_DEBUG == 1

	GLenum error = glGetError();
	if ( error != GL_NO_ERROR ) {
		char buffer[2048];
		
		if ( s ) {
			snprintf(buffer, sizeof(buffer), "%s\n%s line %d: %s", s, file, line, rbunny_err_to_string(error) );
		}
		else {
			snprintf(buffer, sizeof(buffer), "%s line %d: %s", file, line, rbunny_err_to_string(error) );
		}
		
		fatalError(buffer);
		
	}
	return error;
#else
#warning debugging surpressed
	return GL_NO_ERROR;
#endif
	
}

const char *rbunny_err_to_string(GLenum errorCode) {
	
	switch (errorCode) {
		case GL_NO_ERROR:			return "GL_NO_ERROR";			break;
		case GL_INVALID_ENUM:		return "GL_INVALID_ENUM";		break;
		case GL_INVALID_VALUE:		return "GL_INVALID_VALUE";		break;
		case GL_INVALID_OPERATION:	return "GL_INVALID_OPERATION";	break;
		case GL_STACK_OVERFLOW:		return "GL_STACK_OVERFLOW";		break;
		case GL_STACK_UNDERFLOW:	return "GL_STACK_UNDERFLOW";	break;
		case GL_OUT_OF_MEMORY:		return "GL_OUT_OF_MEMORY";		break;
		default:
			return "invalid error code!";	break;
	}
	
}

#endif